package com.ruoyi.project.jiaqin.quotationclass.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.framework.web.domain.Ztree;
import com.ruoyi.framework.web.domain.ZtreeStr;
import com.ruoyi.project.jiaqin.quotationclass.utils.PinYinUtils;
import com.ruoyi.project.system.dept.domain.Dept;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jiaqin.quotationclass.domain.QuotationClass;
import com.ruoyi.project.jiaqin.quotationclass.service.IQuotationClassService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 行情类型管理Controller
 *
 * @author admin
 * @date 2020-12-21
 */
@Controller
@RequestMapping("/jiaqin/quotationclass")
public class QuotationClassController extends BaseController
{

    private String prefix = "jiaqin/quotationclass";

    @Autowired
    private IQuotationClassService quotationClassService;



    /**
     * 去选择分类树页
     *
     * @param deptId 部门ID
     * @param excludeId 排除ID
     */
    @GetMapping(value = { "/selectQuotationClassTree/{quotationClassId}", "/selectQuotationClassTree/{quotationClassId}/{excludeId}" })
    public String selectDeptTree(@PathVariable("quotationClassId") String quotationClassId,
                                 @PathVariable(value = "excludeId", required = false) String excludeId, ModelMap mmap)
    {
        mmap.put("quotationClass", quotationClassService.selectQuotationClassById(quotationClassId));//查分类名
        mmap.put("excludeId", excludeId);
        return prefix + "/tree";
    }

    /**
     * 加载分类列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<ZtreeStr> treeData() {
        List<ZtreeStr> ztrees = quotationClassService.selectQuotationClassTree();
        return ztrees;
    }

    @RequiresPermissions("jiaqin:quotationclass:view")
    @GetMapping()
    public String quotationclass()
    {
        return prefix + "/quotationclass";
    }

    /**
     * 查询一级分类列表
     */
    @RequiresPermissions("jiaqin:quotationclass:list")
    @PostMapping("/listOne")
    @ResponseBody
    public TableDataInfo listOne(QuotationClass quotationClass)
    {
        quotationClass.setLevel(0);
        startPage();
        List<QuotationClass> list = quotationClassService.selectQuotationClassList(quotationClass);
        return getDataTable(list);
    }

    /**
     * 查询二级分类列表
     */
    @RequiresPermissions("jiaqin:quotationclass:list")
    @PostMapping("/listTwo")
    @ResponseBody
    public TableDataInfo listTwo(QuotationClass quotationClass)
    {
        startPage();
        List<QuotationClass> list = quotationClassService.selectQuotationClassList(quotationClass);
        return getDataTable(list);
    }

    /**
     * 查询三级分类列表
     */
    @RequiresPermissions("jiaqin:quotationclass:list")
    @PostMapping("/listThr")
    @ResponseBody
    public TableDataInfo listThr(QuotationClass quotationClass)
    {
        startPage();
        List<QuotationClass> list = quotationClassService.selectQuotationClassList(quotationClass);
        return getDataTable(list);
    }



    /**
     * 去新增一级分类页
     */
    @GetMapping("/addOne")
    public String addOne()
    {
        return prefix + "/add";
    }

    /**
     * 添加一级分类接口
     */
    @RequiresPermissions("jiaqin:quotationclass:add")
    @Log(title = "行情类型管理", businessType = BusinessType.INSERT)
    @PostMapping("/addOne")
    @ResponseBody
    public AjaxResult addSave(QuotationClass quotationClass)
    {
        quotationClass.setCreateAt(new Date());
        quotationClass.setLevel(0);
        quotationClass.setParentId("0");
        quotationClass.setInitials(PinYinUtils.hanziToPinyin(quotationClass.getName(),"").substring(0,1));
        return toAjax(quotationClassService.insertQuotationClass(quotationClass));
    }




    /**
     * 去二级分类列表页
     * @param oneId
     * @param model
     * @return
     */
    @RequiresPermissions("jiaqin:quotationclass:list")
    @GetMapping("/toTwo/{oneId}")
    public String toTwo(@PathVariable("oneId") String oneId, Model model) {
        //根据一级分类ID查询二级分类列表
        model.addAttribute("oneId",oneId);
        return prefix + "/quotationclasstwo";
    }

    /**
     * 去二级分类新增页
     */
    @GetMapping("/addTwo/{oneId}")
    public String addTwo(@PathVariable("oneId") String oneId ,Model model)
    {
        model.addAttribute("oneId",oneId);
        return prefix + "/addTwo";
    }
    /**
     * 添加二级分类接口
     */
    @RequiresPermissions("jiaqin:quotationclass:add")
    @Log(title = "行情类型管理", businessType = BusinessType.INSERT)
    @PostMapping("/addTwo")
    @ResponseBody
    public AjaxResult addTwoSave(QuotationClass quotationClass)
    {
        quotationClass.setCreateAt(new Date());
        quotationClass.setLevel(1);
        quotationClass.setInitials(PinYinUtils.hanziToPinyin(quotationClass.getName(),"").substring(0,1));
        return toAjax(quotationClassService.insertQuotationClass(quotationClass));
    }

    /**
     * 去三级分类列表页
     * @param oneId
     * @param model
     * @return
     */
    @RequiresPermissions("jiaqin:quotationclass:list")
    @GetMapping("/toThr/{twoId}")
    public String toThr(@PathVariable("twoId") String twoId, Model model) {
        //根据一级分类ID查询二级分类列表
        model.addAttribute("twoId",twoId);
        return prefix + "/quotationclassthr";
    }
    /**
     * 去三级分类新增页
     */
    @GetMapping("/addThr/{twoId}")
    public String addThr(@PathVariable("twoId") String twoId ,Model model)
    {
        model.addAttribute("twoId",twoId);
        return prefix + "/addThr";
    }
    /**
     * 添加三级分类接口
     */
    @RequiresPermissions("jiaqin:quotationclass:add")
    @Log(title = "行情类型管理", businessType = BusinessType.INSERT)
    @PostMapping("/addThr")
    @ResponseBody
    public AjaxResult addThrSave(QuotationClass quotationClass)
    {
        quotationClass.setCreateAt(new Date());
        quotationClass.setLevel(2);
        quotationClass.setInitials(PinYinUtils.hanziToPinyin(quotationClass.getName(),"").substring(0,1));
        return toAjax(quotationClassService.insertQuotationClass(quotationClass));
    }


    /**
     * 导出行情类型管理列表
     */
    @RequiresPermissions("jiaqin:quotationclass:export")
    @Log(title = "行情类型管理", businessType = BusinessType.EXPORT)
    @PostMapping("/exportOne")
    @ResponseBody
    public AjaxResult export(QuotationClass quotationClass)
    {
        List<QuotationClass> list = quotationClassService.selectQuotationClassList(quotationClass);
        ExcelUtil<QuotationClass> util = new ExcelUtil<QuotationClass>(QuotationClass.class);
        return util.exportExcel(list, "quotationclass");
    }

    /**
     * 去一级分类修改页面
     */
    @GetMapping("/editOne/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        QuotationClass quotationClass = quotationClassService.selectQuotationClassById(id);
        mmap.put("quotationClass", quotationClass);
        return prefix + "/edit";
    }

    /**
     * 去二级分类修改页面
     */
    @GetMapping("/editTwo/{id}")
    public String editTwo(@PathVariable("id") String id, ModelMap mmap)
    {
        QuotationClass quotationClass = quotationClassService.selectQuotationClassById(id);
        mmap.put("quotationClass", quotationClass);
        return prefix + "/editTwo";
    }

    /**
     * 去三级分类修改页面
     */
    @GetMapping("/editThr/{id}")
    public String editThr(@PathVariable("id") String id, ModelMap mmap)
    {
        QuotationClass quotationClass = quotationClassService.selectQuotationClassById(id);
        mmap.put("quotationClass", quotationClass);
        return prefix + "/editThr";
    }

    /**
     * 修改各级分类
     */
    @RequiresPermissions("jiaqin:quotationclass:edit")
    @Log(title = "行情类型管理", businessType = BusinessType.UPDATE)
    @PostMapping("/editAll")
    @ResponseBody
    public AjaxResult editSave(QuotationClass quotationClass) {
//        System.out.println(quotationClass);
        quotationClass.setUpdateAt(new Date());
        quotationClass.setInitials(PinYinUtils.hanziToPinyin(quotationClass.getName(),"").substring(0,1));
        return toAjax(quotationClassService.updateQuotationClass(quotationClass));
    }

    /**
     * 删除分类
     */
    @RequiresPermissions("jiaqin:quotationclass:remove")
    @Log(title = "行情类型管理", businessType = BusinessType.DELETE)
    @PostMapping( "/removeAll")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(quotationClassService.deleteQuotationClassByIds(ids));
    }
}
